﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.DTOs.WhiteStripWeighingHtmlGroup;

namespace Butcher.winform.EnterpriseForms.Fatoiles
{
    public partial class FatoilAdd : Form
    {
        public FatoilAdd()
        {
            InitializeComponent();
            DateTime dateTime = DateTime.Now;
            datetimes = Convert.ToDateTime(uiDatePicker1.Text);
            //获取当前月份天数
            days = Thread.CurrentThread.CurrentUICulture.Calendar.GetDaysInMonth(datetimes.Year, datetimes.Month);

            getLoad();
        }
        public static int days = 0;
        public DateTime datetimes;

        UIFlowLayoutPanel[] uiflow = new UIFlowLayoutPanel[days];
        public class Timedto
        {
            /// <summary>
            /// 时间
            /// </summary>
            public DateTime? Date { get; set; }
        }
        public void getLoad()
        {
            string url = @"http://localhost:5229/api/Ending/getGrouuWight";
            HttpClient client = new HttpClient();
            Timedto res = new Timedto();
            res.Date = Convert.ToDateTime(uiDatePicker1.Text);
            var content = new StringContent(JsonSerializer.Serialize<Timedto>(res));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var aaa = info["date"].Value<JArray>();
            List<WeighTheFatGroupList> enterprises = aaa.ToObject<List<WeighTheFatGroupList>>();
            for (int i = 0; i < days; i++)
            {
                int Nums = 0;
                int WeghitNums = 0;
                decimal WightSum = 0;
                foreach (var item in enterprises)
                {
                    if (item.Day == (i + 1))
                    {
                        Nums = item.SlaughterNum;
                        WeghitNums = item.WeighNum;
                        WightSum = item.Day;
                    }
                }
                UIFlowLayoutPanel uiflowd = new UIFlowLayoutPanel();
                uiflowd.Size = new Size(250, 150);
                for (int c = 0; c <= 3; c++)
                {
                    Label newlabel = new Label();
                    Label newllabel2 = new Label();
                    newlabel.Width = 150;
                    newllabel2.Width = 80;
                    if (c == 0)
                    {
                        newlabel.Text = (i + 1).ToString();
                    }
                    else if (c == 1)
                    {
                        newlabel.Text = "计划屠宰头数：";
                        newllabel2.Text = Nums.ToString();
                    }
                    else if (c == 2)
                    {
                        newlabel.Text = "称重头数：";
                        newllabel2.Text = WeghitNums.ToString();
                    }
                    else if (c == 3)
                    {
                        newlabel.Text = "称重重量:";
                        newllabel2.Text = WightSum.ToString();
                    }
                    uiflowd.Controls.Add(newlabel);
                    uiflowd.Controls.Add(newllabel2);
                }
                uiFlowLayoutPanel1.Controls.Add(uiflowd);
            }
        }

        private void uiDatePicker1_ValueChanged(object sender, DateTime value)
        {
            uiFlowLayoutPanel1.Clear();
            datetimes = Convert.ToDateTime(uiDatePicker1.Text);
            //获取当前月份天数
            days = Thread.CurrentThread.CurrentUICulture.Calendar.GetDaysInMonth(datetimes.Year, datetimes.Month);
            getLoad();
        }
    }
}
